<?php
namespace common\modules\mapp\backend\models\search;

use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\modules\mapp\models\MappVersionUpgrade;

/**
 * MappVersionUpgradeSearch represents the model behind the search form about `common\modules\mapp\models\MappVersionUpgrade`.
 */
class MappVersionUpgradeSearch extends MappVersionUpgrade
{

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [
                [
                    'id',
                    'app_id',
                    'version_major',
                    'version_mini',
                    'upgrade_type',
                    'status',
                    'created_by',
                    'created_at'
                ],
                'integer'
            ],
            [
                [
                    'version_code',
                    'download_url',
                    'upgrade_hint'
                ],
                'safe'
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params            
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = MappVersionUpgrade::find();
        
        $dataProvider = new ActiveDataProvider([
            'query' => $query
        ]);
        
        if (! ($this->load($params) && $this->validate())) {
            return $dataProvider;
        }
        
        $query->andFilterWhere([
            'id' => $this->id,
            'app_id' => $this->app_id,
            'version_major' => $this->version_major,
            'version_mini' => $this->version_mini,
            'upgrade_type' => $this->upgrade_type,
            'status' => $this->status,
            'created_by' => $this->created_by,
            'created_at' => $this->created_at
        ]);
        
        $query->andFilterWhere([
            'like',
            'version_code',
            $this->version_code
        ])
            ->andFilterWhere([
            'like',
            'download_url',
            $this->download_url
        ])
            ->andFilterWhere([
            'like',
            'upgrade_hint',
            $this->upgrade_hint
        ]);
        
        return $dataProvider;
    }
}
